package com.web.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.annotation.CurrentUser;
import com.web.annotation.LoginRequired;
import com.web.domain.Response;
import com.web.entity.*;
import com.web.service.HomeService;
import com.web.service.SupplyService;
import com.web.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应
 */

@Slf4j
@RestController
@RequestMapping(value = "/app")
public class SupplyController extends BaseController{

    @Autowired
    private SupplyService supplyService;
    @Autowired
    private HomeService homeService;
    /**
     * 发布供应
     * @param info
     * @return
     */
    @PostMapping("/job/addSupply")
    public Response addSupply(@CurrentUser UserInfo info, Supply supply){
        int type = getTechInfo(info.getUserId());
        if(type == 2){
            return Response.custom().failure("请完善企业信息再发布供应信息信息！");
        }
        if( StringUtils.isBlank(supply.getTitle()) || StringUtils.isBlank(supply.getContact_info()) ||
            StringUtils.isBlank(supply.getLatitude()) || StringUtils.isBlank(supply.getLongitude()) ||
            StringUtils.isBlank(supply.getSupply_desc()) ||  StringUtils.isBlank(supply.getPhotos())){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }
        supply.setUser_id(info.getUserId());
        supply.setTitle(supply.getTitle());
        supply.setContact_info(supply.getContact_info());
        supply.setLatitude(supply.getLatitude());
        supply.setLongitude(supply.getLongitude());
        supply.setSupply_desc(supply.getSupply_desc());
        supply.setVideos(supply.getVideos());
        supply.setFirst_video_photos(supply.getFirst_video_photos());

        supply.setPhotos(supply.getPhotos());
        supply.setCreate_time(new Date());
        supply.setModify_time(new Date());
        supplyService.insertSelective(supply);
        return Response.custom().ok("发布成功！");
    }


    /**
     * 供应详情
     * @param info
     * @param id
     * @return
     */
    @PostMapping("/job/SupplyDetails")
    public Response SupplyDetails(@CurrentUser UserInfo info, Integer id,String user_id){
        if (id == null && user_id ==null){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }
        if (supplyService.getSupplyDetails(id,info.getUserId()) == null) {
            return Response.custom().failure("未找到该供应!");
        } else {
            //判断用户是否点亮该供应
            Integer supplyLightenTimes = supplyService.countSupplyLighten(info.getUserId(), id);
            Map<String,Object> map = new HashedMap();
            if (supplyLightenTimes < 3) {
                map.put("supplyLightenTimes", supplyLightenTimes);
                map.put("isLighten", false);
                map.put("supplyDetails",supplyService.getSupplyDetails(id,info.getUserId()));
            } else {
                map.put("isLighten", true);
                map.put("supplyDetails",supplyService.getSupplyDetails(id,info.getUserId()));
            }
            map.put("SupplyList",supplyService.findUserSupplyList(user_id));
            return Response.Map(map).ok();
        }
    }



    /**
     * 供应列表
     * @param pageNum
     * @return
     */
    @LoginRequired
    @PostMapping("/job/SupplyList")
    public Response SupplyList(Integer pageNum){
        if (pageNum == null){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }
        PageHelper.startPage(pageNum,10);
        List<SupplyList> list = supplyService.getSupplyList();
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return Response.custom().ok().addParams(new List[0]);
        }else{
            return Response.custom().ok().addParams(pageInfo.getList());
        }

    }

    /***
     * 四闲类型
     * @param info
     * @return
     */
    @PostMapping("/job/LeaveuNused")
    public Response LeaveuNused(@CurrentUser UserInfo info){

        List<LeaveUnused> list = supplyService.LeaveuNusedList();
        return Response.custom().ok().addParams(list);
    }


    /**
     * 发布四闲
     * @param info
     * @return
     */
    @PostMapping("/job/ReleaseFourIdles")
    public Response ReleaseFourIdles(@CurrentUser UserInfo info,int lu_id,String title,String mobile,String lu_start_time,String lu_over_time,
                                     String address,String work_experience,String lu_describe,String videos, String photos,
                                     String first_video_photos) throws ParseException {

        if( StringUtils.isBlank(String.valueOf(lu_id)) || StringUtils.isBlank(lu_start_time) || StringUtils.isBlank(lu_over_time) ||
                StringUtils.isBlank(title) ||  StringUtils.isBlank(address) || StringUtils.isBlank(lu_describe) ||
                StringUtils.isBlank(mobile)){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }


        LeaveUnusedInfo leaveUnusedInfo = new LeaveUnusedInfo();
        leaveUnusedInfo.setUser_id(info.getUserId());
        leaveUnusedInfo.setTitle(title);
        leaveUnusedInfo.setMobile(mobile);
        leaveUnusedInfo.setLu_start_time(DateUtil.dateFormat(Long.valueOf(lu_start_time)));
        leaveUnusedInfo.setLu_over_time(DateUtil.dateFormat(Long.valueOf(lu_over_time)));
        leaveUnusedInfo.setAddress(address);
        leaveUnusedInfo.setWork_experience(work_experience);
        leaveUnusedInfo.setLu_describe(lu_describe);
        leaveUnusedInfo.setPhotos(photos);
        leaveUnusedInfo.setFirst_video_photos(first_video_photos);
        leaveUnusedInfo.setVideos(videos);
        leaveUnusedInfo.setLu_id(lu_id);
        leaveUnusedInfo.setShare_num(0);
        leaveUnusedInfo.setSee_num(0);
        leaveUnusedInfo.setCreate_time(new Date());
        leaveUnusedInfo.setModify_time(new Date());
        supplyService.insertReleaseFourIdles(leaveUnusedInfo);
        return Response.custom().ok("发布成功！");
    }

    /**
     * 四闲列表
     * @param info
     * @param
     * @param pageNum
     * @return
     */
    @PostMapping("/job/getFourIdleLists")
    public Response getFourIdleLists(@CurrentUser UserInfo info,Integer lu_id,Integer pageNum){
        if (pageNum == null){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }

        PageHelper.startPage(pageNum,10);
        List<FourIdleLists> list = supplyService.getFourIdleLists(lu_id);
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return Response.custom().ok().addParams(new List[0]);
        }else{
            return Response.custom().ok().addParams(pageInfo.getList());
        }
    }


    /**
     * 获取四闲详情
     * @param info
     * @param id
     * @return
     */
    @PostMapping("/job/getFourIdleDetails")
    public Response getFourIdleDetails(@CurrentUser UserInfo info,int id ){
        if (StringUtils.isBlank(String.valueOf(id))){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("details",supplyService.getFourIdleDetails(id,info.getUserId()));
        String topicId = String.valueOf(id);
        map.put("comment",homeService.getInstantLifeComment("2",topicId));
        return Response.custom().ok().addParams(map);
    }


}
